package dd.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import client.ServerAccess;
import dd.view.SignInScreen;

/**
 * fake sever database
 */
public class serverModel {

	// key: eventId, value: a list of client id
	// save the event (event id) and its clients (client id)
	private Map<String, Set<String>> eventIdClientIdsMap;

	// key: eventId, value: Event
	// save the event (event id) and event bean
	private Map<String, Event> eventIdEventMap;

	Event event;
	SignInScreen sins;
	String userid[] = new String[10];
	int numofuserin = 0;

	private static serverModel instance = null;
	/** Connection to server. */
	ServerAccess access;

	private serverModel() {
		this.eventIdClientIdsMap = new HashMap<String, Set<String>>();
		this.eventIdEventMap = new HashMap<String, Event>();

		// TODO remove after completed...
		event = new Event();
	}

	// singletion class, it means the instance in server is only one, but must have capability to record all the events
	// along with their clientS
	public static serverModel getInstance() {
		if (instance == null) {
			instance = new serverModel();
		}
		return instance;
	}

	// get the clients under one event from the server
	public Set<String> getClientIdsByEventId(String eventId) {
		if (eventId == null) {
			return null;
		}
		return this.eventIdClientIdsMap.get(eventId);
	}

	// get one event from server
	public Event getEventByEventId(String eventId) {
		if (eventId == null) {
			return null;
		}
		return this.eventIdEventMap.get(eventId);
	}

	// update client under one event in server
	public void updateEventIdClientIds(String eventId, String clientId) {
		Set<String> clients = this.getClientIdsByEventId(eventId);
		if (clients == null) {
			// set the new value
			clients = new HashSet<String>();
			clients.add(clientId);
		} else {
			// remove the old value
			this.eventIdClientIdsMap.remove(eventId);
			clients.add(clientId); // regardless the duplicates
		}
		this.eventIdClientIdsMap.put(eventId, clients);
	}

	// update event
	public void updateEventIdEvent(String eventId, Event event) {
		Event e = this.getEventByEventId(eventId);
		if (e != null) {
			this.eventIdEventMap.remove(eventId);
		}
		this.eventIdEventMap.put(eventId, event);
	}

	// remove a event
	public boolean removeAnEvent(String eventId) {
		if (this.eventIdClientIdsMap.containsKey(eventId)) {
			this.eventIdClientIdsMap.remove(eventId);
		}
		if (this.eventIdEventMap.containsKey(eventId)) {
			this.eventIdEventMap.remove(eventId);
		}
		return true;
	}

	public Map<String, Event> getEventIdEventMap() {
		return eventIdEventMap;
	}

	public void setEvent(Event e) {
		this.event = e;
	}

	public Event getEvent() {
		return event;
	}

	/** Return the server access object. */
	public ServerAccess getServerAccess() {
		return access;
	}

	/** Application needs to know the access object for contacting server. */
	public void setServerAccess(ServerAccess sa) {
		this.access = sa;
	}

	public void setSignInScreen(SignInScreen sis) {
		this.sins = sis;
	}

	public SignInScreen getSignInScreen() {
		return sins;
	}

	public void setuserid(int i, String id) {
		userid[i] = id;
	}

	public String getuserid(int i) {
		return userid[i];
	}

	public void setnumofuserin(int i) {
		numofuserin = i;
	}

	public int getnumofuserin() {
		return numofuserin;
	}
}
